package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Slf4j
@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public int save(Comment comment) {
        comment.setId(ObjectId.get());
        comment.setCreated(System.currentTimeMillis());
        mongoTemplate.insert(comment);
        return updateCount(comment, 1);
    }

    @Override
    public int remove(Comment comment) {
        Query query = new Query();
        Criteria criteria = Criteria.where("publishId")
                .is(comment.getPublishId())
                .and("pubType").is(comment.getPubType())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId());
        query.addCriteria(criteria);
        mongoTemplate.remove(query, Comment.class);

        return updateCount(comment, -1);
    }

    @Override
    public PageResult findComments(int page, int pagesize, String publishId) {

        //查询条件 publishId动态id， commentType评论类型2（1是点赞，2是文字评论，3是喜欢
        Query query = new Query();
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(2);

        //设置分页排序
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Order.desc("created")));
        query.with(PageRequest.of(page - 1, pagesize));

        //查询
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult<>((int) count, pagesize, pages, page, commentList);
    }

    private Integer updateCount(Comment comment, int value) {
        Integer pubType = comment.getPubType();
        if (pubType == 1) {
            String column = comment.getCountColumn();
            Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
            Update update = new Update().inc(column, value);
            FindAndModifyOptions options = new FindAndModifyOptions();
            options.returnNew(true);
            Publish publish = mongoTemplate.findAndModify(query, update, options, Publish.class);
            Integer commentType = comment.getCommentType();
            if (commentType == 1) {
                return publish.getLikeCount();
            } else if (commentType == 2) {
                return publish.getCommentCount();
            } else if (commentType == 3) {
                return publish.getLoveCount();
            }
        } else if (pubType == 2) {
            Integer commentType = comment.getCommentType();
            if (commentType == 1) {
                //todo 点赞:对视频点赞数+1
            } else if (commentType == 2) {
                //todo 文字评论:对视频的文字评论数+1
            }
        } else if (pubType == 3) {
            Integer commentType = comment.getCommentType();
            //todo 是评论
        }
        return null;

    }


}